/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.anniversary.domain.Leader;
import me.zhengjie.modules.anniversary.repository.LeaderRepository;
import me.zhengjie.modules.anniversary.service.LeaderService;
import me.zhengjie.modules.anniversary.service.dto.LeaderDto;
import me.zhengjie.modules.anniversary.service.dto.LeaderQueryCriteria;
import me.zhengjie.modules.anniversary.service.mapstruct.LeaderMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Jiayin Wei
* @date 2020-07-27
**/
@Service
@RequiredArgsConstructor
public class LeaderServiceImpl implements LeaderService {

    private final LeaderRepository leaderRepository;
    private final LeaderMapper leaderMapper;

    @Override
    public Map<String,Object> queryAll(LeaderQueryCriteria criteria, Pageable pageable){
        Page<Leader> page = leaderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(leaderMapper::toDto));
    }

    @Override
    public List<LeaderDto> queryAll(LeaderQueryCriteria criteria){
        return leaderMapper.toDto(leaderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LeaderDto findById(Long id) {
        Leader leader = leaderRepository.findById(id).orElseGet(Leader::new);
        ValidationUtil.isNull(leader.getId(),"Leader","id",id);
        return leaderMapper.toDto(leader);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LeaderDto create(Leader resources) {
        return leaderMapper.toDto(leaderRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Leader resources) {
        Leader leader = leaderRepository.findById(resources.getId()).orElseGet(Leader::new);
        ValidationUtil.isNull( leader.getId(),"Leader","id",resources.getId());
        leader.copy(resources);
        leaderRepository.save(leader);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            leaderRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<LeaderDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (LeaderDto leader : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", leader.getName());
            map.put("教职工校友ID", leader.getTeacherId());
            map.put("性别（0：男，1：女）", leader.getGender());
            map.put("出生年月", leader.getBirthDate());
            map.put("民族_id", leader.getNationId());
            map.put("政治面貌_id", leader.getPoliticalId());
            map.put("职务", leader.getJob());
            map.put("任职起始日期", leader.getJobStartDate());
            map.put("任职结束日期", leader.getJobEndDate());
            map.put("生活照保存路径", leader.getPhotoPath());
            map.put("简介", leader.getBrief());
            map.put("创建者名称", leader.getCreateBy());
            map.put("创建时间", leader.getCreateTime());
            map.put("修改者名称", leader.getUpdatedBy());
            map.put("修改时间", leader.getUpdateTime());
            map.put("排序", leader.getSort());
            map.put("备注", leader.getRemark());
            map.put("是否启用", leader.getIsEnable());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}